<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <scroll class="content" 
            ref="scroll" 
            :probe-type='3'
            @scrollInfo="getScrollInfo"
            @pullingUP="loadMore"
    >
      <home-swiper :banners="banners"/>
      <recommends-view :recommends='recommends'/>
      <feature-view/>
      <tab-control class="tab-control"
      :titles='["流行","新款","精选"]'
      @tabClick='tabClick'/>
      <goods-list :goodsList='goodsList'></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="iconIsShow"/>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar"
import TabControl from 'components/content/tabcontrol/TabControl.vue';
import GoodsList from "components/content/goods/GoodsList.vue"
import Scroll from "components/common/scroll/Scroll.vue"
import BackTop from 'components/content/backtop/BackTop.vue'

import HomeSwiper from "./childComps/HomeSwiper.vue"
import RecommendsView from './childComps/RecommendsView.vue'
import FeatureView from './childComps/FeatureView.vue'

import {getHomeMultidata,getHomeGoods} from 'network/home.js'
export default {
  name:'Home',
  components:{
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,

    HomeSwiper,
    RecommendsView,
    FeatureView,
    BackTop,
    GoodsList,
    TabControl
  },
  data(){
    return {
      banners:[],
      // dKeyword:[],
      // keywords:[],
      recommends:[],
      goods:{
        "pop":{page:0,list:[]},
        "new":{page:0,list:[]},
        "sell":{page:0,list:[]},
      },
      currentType:"pop",
      iconIsShow:false
    }
  },
  computed:{
    goodsList(type){
      return this.goods[this.currentType].list
    }
  },
  created(){
    this.getHomeMultidata()
    this.getHomeGoods("pop")
    this.getHomeGoods("new")
    this.getHomeGoods("sell")
  },
  methods:{
    //上拉加载更多
    loadMore(){
      // console.log("上拉加载更多")
      this.getHomeGoods(this.currentType)
    },
    //得到滚动的信息
    getScrollInfo(position){
      this.iconIsShow=(-position.y)>1000
    },
    //回到顶部
    backClick(){
      // console.log('lalala')
      // console.log(this.$refs.scroll.message)
      this.$refs.scroll.scrollTo(0,0)
    },
    //tab点击监听
    tabClick(index){
      switch(index){
        case 0:
          this.currentType='pop'
          break
        case 1:
          this.currentType='new'
          break
        case 2:
          this.currentType='sell'
          break
      }

    },

    //网络请求相关方法
    //在组件创建之后请求数据
    getHomeMultidata(){
      getHomeMultidata().then(res=>{
      this.banners=res.data.data.banner.list
      this.recommends=res.data.data.recommend.list
    })
    },
    //请求商品数据
    getHomeGoods(type){
      const page=this.goods[type].page+1
      getHomeGoods(type,page).then(res=>{
      // console.log(res.data.data.list)
      this.goods[type].list.push(...res.data.data.list)
      this.goods[type].page+=1
      this.$refs.scroll.finishPullUp()
    })
    }
  }

}
</script>

<style scoped>
  #home{
    /* padding-top: 44px; */
    height: 100vh;
    /* position: relative; */
  }
  .home-nav{
    background-color: var(--color-tint);
    color: white;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 999;
  }
  .tab-control{
    position: sticky;
    top:44px;
    background-color: #fff;
    z-index: 999;
  }
  /* .content{
    position: absolute;
    top:44px;
    bottom: 49px;
    left: 0;
    right: 0;
    overflow: hidden;
  } */
  .content{
    height: calc(100% - 93px);
    overflow: hidden;
    margin-top: 44px;

  }
</style>